﻿using System;
using System.Web.Mvc;
using System.Configuration;
using System.Web.Security;
using mBlog.Services;
using mBlog.ViewModels;
using mBlog.Models;
using mBlog.Extensions;

namespace mBlog.Controllers
{
    public class HomeController : BaseController
    {
        private IPostService postService;

        public HomeController(IPostService ps)
        {
            this.postService = ps;
        }

        public ActionResult Index()
        {
            Post post = postService.First();

            return RedirectToAction("View", "Post", new { urlName = post.UrlName });
        }

        public ActionResult Page(int page)
        {
            HomeView homeView = new HomeView {Posts = postService.Get(page, ItemsPerPage)};


            PagerView pager = new PagerView
                                  {
                                      PageCount = (int) Math.Ceiling((double) postService.Count()/ItemsPerPage),
                                      CurrentPage = page,
                                      ActionName = "HomePaging",
                                      ControllerName = "Home"
                                  };

            homeView.PagerView = pager;

            return View("Index", homeView);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Footer()
        {
            FooterView footView = new FooterView
                                      {
                                          RecentPosts = postService.Get(0, 5).MapTo<PostDetail>(), 
                                          RecentComments = postService.GetRecentComment(5).MapTo<CommentDetail>()
                                      };
            return PartialView("_Footer", footView);
        }

        //[ChildActionOnly]
        //public ActionResult Sidebar()
        //{
        //    SidebarView sidebar = new SidebarView();

        //    ITwitterService twitterService = new TwitterService();
        //    sidebar.Tweets = new List<string>();
        //    try
        //    {
        //        sidebar.Tweets = twitterService.GetTweets().Take(8).ToList();
        //    }
        //    catch { }
        //    sidebar.Categories = categoryService.Get();
        //    sidebar.RecentPosts = postService.Get(0, ItemsPerPage);

        //    return PartialView("_SidebarPartial", sidebar);
        //}

        [HttpGet]
        public ActionResult Logon()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Logon(string name, string password)
        {
            if (name == "jun1st" && password == ConfigurationManager.AppSettings["Password"])
            {
                FormsAuthentication.SetAuthCookie("jun1st", false);
                return RedirectToAction("Index", "Admin");
            }
            else
            {
                return RedirectToAction("Logon");
            }
        }
    }
}
